<template>
  <div class="container">
    <div class="head">
      <img :src="item.img" />
    </div>
    <div class="mess">
      <img
        src="https://img1.baidu.com/it/u=4256350010,1509363888&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400"
        class="avatar"
        style="
          width: 100px;
          height: 100px;
          border-radius: 50%;
          background-color: black;
        "
      />
      <div class="title">{{ item.title }}</div>
      <div class="name">{{ item.author }}</div>
      <div class="collect" @click="collect">
        <el-icon size="40" :class="{ active: flag }"><StarFilled /></el-icon>收藏
      </div>
    </div>
    <div class="line"></div>
    <div class="main">
      <div class="box">
        <el-icon color="#FDD692" size="25px"><Clock /></el-icon>
        <div>出发时间：{{ item.date }}</div>
      </div>
      <div class="box">
        <el-icon color="#FDD692" size="25px"><Sunrise /></el-icon>
        <div>出行天数：{{ item.num }}天</div>
      </div>
      <div class="box">
        <el-icon color="#FDD692" size="25px"><User /></el-icon>
        <div>人物：{{ item.people }}</div>
      </div>
      <div class="box">
        <el-icon color="#FDD692" size="25px"><Money /></el-icon>
        <div>人均费用：{{ item.money }}RMB</div>
      </div>
    </div>
    <div v-html="item.content" class="newcontent"></div>
    <el-divider> End. </el-divider>
    <div class="like">
      <el-rate
        v-model="value"
        :texts="['oops', 'disappointed', 'normal', 'good', 'great']"
        show-text
        size="large"
        allow-half
        @change="change"
      />
      <div class="wenzi">如果觉得我的文章对你有用,请给我评分吧！</div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 接收路由传递的参数
import { useRoute } from 'vue-router'
const $route = useRoute()
const encodedObject = $route.query.item
const item = JSON.parse(decodeURIComponent(encodedObject as string))
import {
  reqlike,
  reqcollect
} from '@/api/strategy/index'
import type {reqtype} from '@/api/strategy/type'
import { ref, onMounted } from 'vue'
let flag = ref(false)
onMounted(() => {
  for (let j = 0; j < item.like.length; j++) {
    if (item.like[j].username == useStore.username) {
      value.value = item.like[j].value
      break
    }
  }
})
import { useUserStore } from '../../stores/user'
let useStore = useUserStore()
let value = ref(0)
import { ElMessage } from 'element-plus'
onMounted(() => {
  for (let i = 0; i < item.collect.length; i++) {
    if (item.collect[i].username == useStore.username) {
      flag.value = true
    }
  }
})

const change = async () => {
  let flag = 0
  for (let i = 0; i < item.like.length; i++) {
    if (item.like[i].username == useStore.username) {
      item.like[i].value = value.value
      flag = 1
      break
    }
  }
  if (!flag) {
    item.like.push({
      id: item.id,
      username: useStore.username,
      value: value.value
    })
  }
  let data={
    id: item.id,
    data: JSON.stringify(item.like)
  }
  let result:reqtype = await reqlike(data)
  if (result.status == 0) {
    ElMessage({
      message: '评星成功！',
      type: 'success'
    })
  }
}

const collect = async () => {
  flag.value = !flag.value
  if (!flag.value) {
    // let index = 0
    for (let i = 0; i < item.collect.length; i++) {
      if (item.collect[i].username == useStore.username) {
        item.collect.splice(i, 1)
        // index = 1
        break
      }
    }
    
    console.log(item.collect)

    if (!item.collect.length) item.collect = []

    let result:reqtype = await reqcollect(item.collect, item.id)
    if (result.status == 0) {
      ElMessage({
        message: '取消收藏成功！',
        type: 'success'
      })
    }
  } else {
    item.collect.push({
      id: item.id,
      username: useStore.username
    })
    let result:reqtype = await reqcollect(item.collect, item.id)
    if (result.status == 0) {
      ElMessage({
        message: '收藏成功！',
        type: 'success'
      })
    }
  }
}
</script>

<style scoped>
.active {
  color: #fdd692;
}
div/deep/.newcontent  p {
  font-weight: 100;
}
div/deep/.newcontent  h1 {
  font-weight: 800;
  font-size: 30px;
  margin: 5px;
}
div/deep/.newcontent p {
  font-weight: 100;
}
div/deep/.newcontent img {
  margin: 5px;
}
.container {
  width: 100%;
  height: 100vh;
  .head {
    width: 100%;
    height: 350px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .mess {
    position: relative;
    .avatar {
      position: absolute;
      top: -50px;
      left: 30%;
    }
    .title {
      position: absolute;
      top: -50px;
      left: 40%;
      font-size: 30px;
      font-weight: 800;
      color: aliceblue;
    }
    .name {
      margin-left: 40%;
      margin-top: 10px;
      font-size: 25px;
      color: #fdd692;
    }
    .collect {
      position: absolute;
      top: 0px;
      left: 70%;
      font-size: 30px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
  .line {
    width: 100%;
    height: 1px;
    margin-top: 30px;
    box-shadow: 0px 8px 2px rgba(0, 0, 0, 0.5); /* 添加阴影，水平偏移为5像素，垂直偏移为5像素，模糊半径为10像素，颜色为半透明黑色 */
  }
  .main {
    margin-left: 30%;
    margin-top: 80px;
    width: 600px;
    height: 100px;
    border: 1px dashed #c5c6b6;
    display: flex;
    flex-wrap: wrap;

    .box {
      width: 33.3%;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 5px;
    }
  }
  .newcontent {
    margin-top: 50px;
    padding: 0px 300px;
    padding-bottom: 30px;
  }
  .like {
    color: #c5c6b6;
    font-size: 13px;
    margin-left: 45%;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-bottom: 100px;
  }
}
</style>
